const app = getApp()
const util = require("../../utils/util")
Page({
  data: {
    page: 0,
    pageSize: 10,
    loadMore: false,
    loadAll: false,
    orderList: []
  },
  onLoad: function (options) {
    this.setData({
      page: 0,
      pageSize: 10,
      loadMore: false,
      loadAll: false
    })
    this.getOrders()
  },
  getOrders () {
    this.setData({
      loadMore: true
    })
    wx.showLoading()
    const params = {
      page: this.data.page,
      pagesize:  this.data.pageSize
    }
    console.log(params)
    const that = this
    app.api.getOrderList(params).then(res => {
      wx.hideLoading()
      this.setData({
        loadMore: false
      })
      console.log(res)
      if (!res.resultCode) {
        const newData = that.data.orderList.concat(res.data)
        newData.forEach(item => {
          item.createtime = util.formatTime(item.createtime)
        })
        that.setData({
          orderList: newData
        })

        if (res.data.length < this.data.pageSize) {
          that.setData({
            loadAll: true
          })
        }
      }
    })
  },
  handleOrderItemClick (e) {
    console.log(e)
    const id = e.currentTarget.dataset.orderid
    console.log(id)
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?id=' + id
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('上拉加载更多')
    console.log(!this.data.loadMore)
    console.log(!this.data.loadAll)
    if (!this.data.loadMore && !this.data.loadAll) {
      this.setData({
        page: this.data.page + 1
      })
      this.getOrders()
    }
  }
});